import axios from 'axios'

//配置基准路径
const baseURL = 'http://localhost:8888/api/private/v1/'
//设置默认的基准路径
axios.defaults.baseURL = baseURL






//添加一个拦截器
//config:可以进行请求头的位置，通过headers属性
//自动调用
axios.interceptors.request.use(function (config) {
  //1获取token
  var token = localStorage.getItem('mytoken')
  if (token) {
    //写入请求头
    config.headers['Authorization'] = token
  }
  return config
}, function (err) {
  return Promise.reject(err)
})





//登录
export const login = (params) => {
  return axios.post('login', params).then((res) => {
    return res.data
  })
}
/*=========== 获取导航数据 ============*/

export const getNavData = () => {
  return axios.get('menus').then(res => {
    return res.data
  })
}

/*=========== 获取导航数据 ============*/
/* -----------用户管理--------------- */
//获取用户列表
export const getUserLists = (pa) => {
  return axios.get('users', {
    params: pa
  }).then((res) => {
    return res.data
  })
}

//添加用户
export const addUser = (params) => {
  return axios.post('users', params).then(res => {
    return res.data
  })
}

//删除用户
export const delUserData = (id) => {
  return axios.delete(`users/${id}`).then(res => {
    return res.data
  })
}
//编辑用户
export const editUser = (id, params) => {
  return axios.put(`users/${id}`, params).then(res => {
    return res.data
  })
}
/* -----------用户管理--------------- */

/* -----------权限管理--------------- */
//获取角色列表
export const getRole = () => {
  return axios.get('roles').then(res => {
    return res.data
  })
}
//角色分配
export const roleName = (params) => {
  return axios.put(`users/${params.id}/role`, {
    rid: params.rid
  }).then(res => {
    return res.data
  })
}

//用户状态
export const userType = (params) => {
  return axios.put(`users/${params.id}/state/${params.mg_state}`).then(res => {
    return res.data
  })
}
//获取权限列表
export const getPowerData = (type) => {
  return axios.get(`rights/${type}`).then(res => {
    return res.data
  })
}
//获取角色权限列表
export const getRolerPowData = () => {
  return axios.get('roles').then(res => {
    return res.data
  })
}
//添加角色
export const addRole = (params) => {
  return axios.post('roles', {
    roleName: params.roleName,
    roleDesc: params.roleDesc
  }).then(res => {
    return res.data
  })
}
//删除角色
export const delRole = (id) => {
  return axios.delete(`roles/${id}`).then(res => {
    return res.data
  })
}
//设置角色权限
export const grantroleright = (pa) => {
  return axios.post(`roles/${pa.roleid}/rights`, {
    rids: pa.rids
  }).then(res => {
    return res.data
  })
}
//删除角色指定权限
export const deleterolerightById = (roleid, rightid) => {
  return axios.delete(`roles/${roleid}/rights/${rightid}`).then(res => {
    return res.data

  })
}
//编辑提交角色
export const editRoleRight = (id, params) => {
  return axios.put(`roles/${id}`, params).then(res => {
    return res.data
  })
}
/* -----------权限管理--------------- */


/* -----------商品管理--------------- */
//获取商品列表
export const getGoods = (pa) => {
  return axios.get('goods', {
    params: pa
  }).then(res => {
    return res.data
  })
}

//删除商品
export const delGoods = (id) => {
  return axios.delete(`goods/${id}`).then(res => {
    return res.data
  })
}
//获取分类列表
export const getCategories = (type) => {
  return axios.get('categories', {
    params: {
      'type': type
    }
  }).then(res => {
    return res.data
  })
}
//添加分类
export const addcate = (params) => {
  return axios.post('categories', params).then(res => {
    return res.data
  })
}

//添加商品
export const addGoods = (pa)=>{
  return axios.post('goods',pa).then(res=>{
    return res.data
  })
}
//删除分类
export const delCategories = (id) => {
  return axios.delete(`categories/${id}`).then(res => {
    return res.data
  })
}
/* -----------商品管理--------------- */
